package com.intgrt.manager.ctrl;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.intgrt.manager.entity.SysOrganize;
import com.intgrt.manager.entity.SysUser;
import com.intgrt.manager.midentity.CombTree;
import com.intgrt.manager.service.SysOrganizeService;
import com.intgrt.manager.util.ProjectConstants;
import com.intgrt.manager.util.UserInfo;

@Controller
public class SysOrganizeCtrl {

	@Autowired
	private SysOrganizeService service;
	private static final String MENU_ID = "6";

	@RequestMapping(value = "/sysorg", method = { RequestMethod.GET })
	public String index(HttpServletRequest request) {
		SysUser curuser = (SysUser)request.getSession().getAttribute(ProjectConstants.SESSION_USER);
		String btnids = "";
		try {
			btnids = curuser.getMenuButtons().getString(MENU_ID);
		} catch (Exception e) {
			btnids = "";
		}
		JSONArray btnlist = service.getMenuBtns(curuser.getId(), MENU_ID, btnids);
		request.setAttribute("btnlist", btnlist);
		return "sysorganize";
	}
	@ResponseBody
	@RequestMapping(value="/d/orglist" , method={RequestMethod.POST})
	public List<Map<String,Object>> list(HttpServletRequest request, SysOrganize entity){
		return service.getList(entity, UserInfo.getOrgid());
	}
	@ResponseBody
	@RequestMapping(value="/d/orgsave" , method={RequestMethod.POST})
	public String menuSave(HttpServletRequest request, SysOrganize entity){
		entity.setParentid(UserInfo.getOrgid());
		return service.saveEntity(entity);
	}
	@ResponseBody
	@RequestMapping(value="/d/orgedit" , method={RequestMethod.POST})
	public String menuEdit(HttpServletRequest request, SysOrganize entity){
		return service.editEntity(entity);
	}
	@ResponseBody
	@RequestMapping(value="/d/orgdel" , method={RequestMethod.POST})
	public String menuDel(HttpServletRequest request){
		String ids = request.getParameter("ids");
		if(ids.equals(UserInfo.getOrgid())) {
			return "1";
		}
		return service.delEntity(ids);
	}
	@ResponseBody
	@RequestMapping(value="/d/orgComBox" , method={RequestMethod.POST})
	public List<CombTree> comBox(HttpServletRequest request){
		String pid = request.getParameter("id");
		return service.getCombTree(pid,UserInfo.getOrgid());
	}
	@ResponseBody
	@RequestMapping(value="/d/recharge" , method={RequestMethod.POST})
	public String recharge(HttpServletRequest request){
		String orgid = request.getParameter("id");
		if(UserInfo.getOrgid().equals(orgid)) {
			return "1";
		}
		SysUser curuser = (SysUser)request.getSession().getAttribute(ProjectConstants.SESSION_USER);
		String cgemoney = request.getParameter("cgemoney");
		String oprtdesc = request.getParameter("oprtdesc");
		return service.recharge(curuser,cgemoney,oprtdesc,orgid);
	}

}